ad = {};

ad.check = function(data){
	if(data['data[UserAddress][address]'] == ''){
		ad.tip('请输入详细地址',false);
		return false;
	}
	if(data['data[UserAddress][receiver]'] == ''){
		ad.tip('请输入收货人',false);
		return false;
	}
	if(data['data[UserAddress][cellphone]'] == ''){
		ad.tip('请输入收货人联系电话',false);
		return false;
	}
	return true;
};

ad.ajax = function(data,callback,action){
	$.ajax({
	  type: "post",
	  url: action,
	  dataType:'json',
	  data:data,
	}).done(function( msg ) {
		callback(msg);
	});
}

ad.submit = function(){
	var data = jQuery('#UserAddressIndexForm').serializeObject();
	if(!ad.check(data)) return;
	var callback = function(res){
		if(res.state){
			jQuery('#u_a_l').append(res.html);
		}
	};
	ad.ajax(data,callback,'UserAddresses/add');
};

ad.del = function(id){
	var callback = function(res){
		if(res.state){
			jQuery('#item-'+res.data).remove();
		}
	};
	data = {id:id};
	ad.ajax(data,callback,'UserAddresses/delete');
};

ad.tip = function(msg,state){
	zmx.tip(msg,state);
	zmx.shineTip();
}

ad.esubmit = function(){
	var data = jQuery('#UserOrderAddForm').serializeObject();
	if(!ad.check(data)) return;
	var callback = function(res){
		if(res.state){
			jQuery('#u_a_l').append(res.html);
			$('#u_a_l input:last').click();
			od.getlogistic(res.data.UserAddress.id);
			jQuery('#UserAddressAddress').val('');
			jQuery('#UserAddressReceiver').val('');
			jQuery('#UserAddressCellphone').val('');
		}
	};
	ad.ajax(data,callback,'UserAddresses/add');
};

